﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace xna_game_engine.source
{
    public class CAnimation
    {

        // This is the index offset for which way we are currently facing
        private int m_nFacingIndex;

        private Rectangle m_sourceRect;
        private Rectangle m_destRect;

        private int m_nNumFrames;
        private int m_nCurrentFrame;

        private int m_nSpriteWidth;
        private int m_nSpriteHeight;

        float timer;
        float interval;

        // TODO : 4 directions for animations all should be in 1 file
        // the algorithm will have to change for that
        public CAnimation(int fps, int sprite_width, int sprite_height, int numFrames)
        {
            timer = 0.0f;
            interval = 1000.0f / fps;
            m_nSpriteHeight = sprite_height;
            m_nSpriteWidth = sprite_width;
            m_nNumFrames = numFrames;
            m_nFacingIndex = 0;

        }


        public void Update(GameTime gameTime, Vector2 dir)
        {
            // Animation order = Right, Down, Left, Up
            if (dir.X > 0)
                m_nFacingIndex = 0;
            else if (dir.Y > 0)
                m_nFacingIndex = m_nNumFrames;
            else if (dir.X < 0)
                m_nFacingIndex = m_nNumFrames * 2;
            else if (dir.Y < 0)
                m_nFacingIndex = m_nNumFrames * 3;

            // update our time
            float deltaTime = (float)gameTime.ElapsedGameTime.TotalMilliseconds;
            timer += deltaTime;

            if (timer > interval)
            {
                m_nCurrentFrame++;
                if (m_nCurrentFrame > m_nNumFrames - 1)
                {
                    m_nCurrentFrame = 0;
                }
                timer = 0.0f;
            }
            m_sourceRect = new Rectangle((m_nCurrentFrame + m_nFacingIndex)* m_nSpriteWidth, 0, m_nSpriteWidth, m_nSpriteHeight);
        }



    #region Properties

        public int SpriteWidth
        {
            get { return m_nSpriteWidth; }
            set { m_nSpriteWidth = value; }
        }

        public int SpriteHeight
        {
            get { return m_nSpriteHeight; }
            set { m_nSpriteHeight = value; }
        }

        public Rectangle SourceRectangle
        {
            get { return m_sourceRect; }
            set { m_sourceRect = value; }
        }

        public Rectangle DestinationRectangle
        {
            get { return m_destRect; }
            set { m_destRect = value; }
        }

    #endregion
    }

}
